package com.gaoxiang.demo.util;

import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * TODO
 *
 * @author gaoxiang
 * @date 2020/9/109:37
 */
@Component
public class EsUtil {
    @Autowired
    private TransportClient client;
    //es插入信息
    public Object testInsert(String index, String type, String id) throws Exception {
        XContentBuilder contentBuilder = XContentFactory.jsonBuilder().startObject()
                .field("title", "Java is very strict language")
                .field("content", "Need learn many")
                .field("postdate", "2019-04-28")
                .field("url", "csdn.net/9090990").endObject();
        IndexResponse response = client.prepareIndex(index,type,id).setSource(contentBuilder).get();
        return response.status();
    }

    //es更新数据
    public Object testUpdate() throws Exception {
        XContentBuilder contentBuilder = XContentFactory.jsonBuilder().startObject()
                .field("title", "Java is not only strict but userful language")
                .field("content", "Too many skills you have to")
                .field("postdate", "2019-05-01")
                .field("url", "csdn.net/9090990").endObject();

        UpdateRequest request = new UpdateRequest();
        request.index("abc").type("blog").id("4").doc(contentBuilder);
        UpdateResponse response = client.update(request).get();
        System.out.println(response.status());
        return response.status();
    }
    //根据es的id删除数据
    public Object testDelete1(String id) {
        DeleteResponse response = client.prepareDelete("abc", "blog", id).get();
        return response.status();
    }
    //根据id查询数据
    public Object getDataById(String id) {
        GetResponse response = client.prepareGet("index1", "blog", id).execute().actionGet();
        System.out.println(response.getSourceAsMap());
        return response.getSourceAsMap();
    }
    //查询所有数据(设置为5条)
    public List<Map<String, Object>> queryAll() {
        List<Map<String, Object>> resMap = new ArrayList<>();
        QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
        SearchResponse response = client.prepareSearch("abc").setQuery(queryBuilder).setSize(5).execute().actionGet();
        SearchHits searchHits = response.getHits();
        for (SearchHit single : searchHits) {
            Map<String, Object> singleMap = single.getSourceAsMap();
            resMap.add(singleMap);
        }
        return resMap;
    }
    public Object testPrefixQuery(){
        List<Map<String,Object>> resMap = new ArrayList<>();
        //prefix查询
        //QueryBuilder queryBuilder = QueryBuilders.prefixQuery("content","PHP");
        //wildcardQuery查询
        //QueryBuilder queryBuilder = QueryBuilders.wildcardQuery("content","PHP");
        //fuzzy 查询，即模糊查询
        //QueryBuilder queryBuilder = QueryBuilders.fuzzyQuery("content","PHP");
        //ids查询
        QueryBuilder queryBuilder = QueryBuilders.idsQuery().addIds("2","7","5","11","13");
        SearchResponse response = client.prepareSearch("abc").setQuery(queryBuilder).setSize(10).get();
        SearchHits hits = response.getHits();
        for(SearchHit single : hits){
            resMap.add(single.getSourceAsMap());
        }
        return resMap;
    }
}
